﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WebManager;
namespace BaoJian.eshop.cart
{
    public partial class cart : System.Web.UI.Page
    {
        private WebProfile Profile
        {
            get { return new WebProfile(Context.Profile); }
        }

        public double TotalPrice;
        public double TotalMarketPrice;
        public string NextURL;
      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    NextURL = "";
                }
                else
                {
                    NextURL = "";
                }

                if (Profile.CustomerSelect == "")
                {
                    return;
                }
                else
                {
                    BindData();
                }
            }
        }


        private void BindData()
        {
            string[] sel;
            if (Profile.IsAnonymous)
            {
                sel = Profile.CustomerSelect.Split(';');
            }
            else
            {
                sel = Profile.CustomerSelect.Split(';');
            }

           

            string sql = null;
            string[] tmp;
            for(int i = 0; i < sel.Length; i++)
            {
                tmp = sel[i].Split(',');
                if(i == 0)
                {
                    sql += " PID=''" + tmp[0] + "'' ";
                }
                else
                {
                    sql += " or  PID=''" + tmp[0] + "''";
                }
            }         

            using (DataSet ds = Tools.Common.GetPage("EProduct","[Name],[PID],[MarketPrice],[Price],0 as NUM","SID ",
                  1000,1,false,true,sql,"PID"))
            {
                DataTable dt = ds.Tables[0];

                for (int s = 0; s < sel.Length; s++)
                {
                    tmp = sel[s].Split(',');
                    for (int t = 0; t < dt.Rows.Count; t++)
                    {
                        if (tmp[0] == dt.Rows[t][1].ToString())
                        {
                            dt.Rows[t][4] = Convert.ToInt32(tmp[1]);

                            TotalPrice += Convert.ToDouble(dt.Rows[t][3]) * Convert.ToDouble(tmp[1]);
                            TotalMarketPrice += Convert.ToDouble(dt.Rows[t][2]) * Convert.ToDouble(tmp[1]);
                            
                            break;
                        }

                       
                    }
                }
                DataList1.DataSource = ds.Tables[0];
                DataList1.DataBind();
            }

            
        }

        protected void UpdateNum_Click(object sender, ImageClickEventArgs e)
        {          
            string[] jg = HttpContext.Current.Request.Form["product_jiage"].Split(',');
            string[] sl = HttpContext.Current.Request.Form["product_shuliang1"].Split(',');
            string[] pid = HttpContext.Current.Request.Form["productID"].Split(',');

            for(int i = 0; i < pid.Length;i++)
            {
                int pos = Profile.CustomerSelect.IndexOf(pid[i]);
                int pos1 = Profile.CustomerSelect.IndexOf(",", pos);
                int pos2 = Profile.CustomerSelect.IndexOf(";", pos1);
                string tmp;
                if (pos2 == -1)  //最后一个商品
                {
                    Profile.CustomerSelect = Profile.CustomerSelect.Remove(pos);
                    Profile.CustomerSelect += pid[i] + "," + sl[i];
                }
                else //选购其他商品
                {
                    tmp = Profile.CustomerSelect.Substring(pos, pos2 - pos);                  
                    Profile.CustomerSelect = Profile.CustomerSelect.Replace(tmp, pid[i] + "," + sl[i]);
                   

                }
            }
            BindData();          
        }
    }
}
